package any.incubator;

import java.util.Map;
import java.util.Map.Entry;

import any.Any;


public abstract class CreateAnyImpl implements CreateAny {

	// TODO: check ob value ein CreateAny ist?
	public CreateAny append(final Object value) {
		return new CreateAnyImpl() {
			public Any apply(Any from) {
				Object val = value;
//				if (value instanceof CreateAny) {
//					val = ((CreateAny)value).apply(Any.create.any()).toMutable();
//				}
				return CreateAnyImpl.this.apply(from).append(val);
			}
		};
	}

	public CreateAny appendAll(final Object values) {
		return new CreateAnyImpl() {
			public Any apply(Any from) {
				return CreateAnyImpl.this.apply(from).appendAll(values);
			}
		};
	}

	public CreateAny appendEach(final Object... values) {
		return new CreateAnyImpl() {
			public Any apply(Any from) {
				return CreateAnyImpl.this.apply(from).appendEach(values);
			}
		};
	}

	public CreateAny put(final String key, final Object value) {
		return new CreateAnyImpl() {
			public Any apply(Any from) {
				return CreateAnyImpl.this.apply(from).put(key, value);
			}
		};
	}

	public CreateAny put(final Entry<?, ?> entry) {
		return new CreateAnyImpl() {
			public Any apply(Any from) {
				return CreateAnyImpl.this.apply(from).put(entry);
			}
		};
	}

	public CreateAny putAll(final Map<?, ?> map) {
		return new CreateAnyImpl() {
			public Any apply(Any from) {
				return CreateAnyImpl.this.apply(from).putAll(map);
			}
		};
	}

	public CreateAny putAll(final Iterable<? extends Entry<?, ?>> entries) {
		return new CreateAnyImpl() {
			public Any apply(Any from) {
				return CreateAnyImpl.this.apply(from).putAll(entries);
			}
		};
	}

	public Any asImmutable() {
		return asAny(false);
	}

	public Any asMutable() {
		return asAny(true);
	}

	public Any asAny(boolean mutable) {
		return apply(Any.create.any()).toAny(mutable);
	}
	
}
